﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Titan;
using StockMatch.Service.Entity;

namespace StockMatch.Service
{
    public class WeekWinProjectService : ContextBoundObject
    {


        #region 增删改

        public bool Insert(WeekWinProject weekWinProject)
        {            
            using (var cn = Environment.OpenSession())
            {
                try
                {
                    cn.BeginTransaction();
                    int i = cn.Insert(weekWinProject);

                    User user = ServiceManager.UserService.GetUser(weekWinProject.UserId);
                    user.LockAmount += weekWinProject.RiskMargin; //冻结金额
                    user.BalanceAmount -= user.LockAmount; //账户余额
                    bool a = ServiceManager.UserService.Update(user, User_.LockAmount, User_.BalanceAmount);

                    if (i > 0 && a)
                    {
                        cn.Commit();
                        weekWinProject.ProjectNumber = string.Format("S{0}", weekWinProject.ProjectId);
                        bool c = Update(weekWinProject, WeekWinProject_.ProjectNumber);
                        return true;
                    }
                    cn.Rollback();
                    return false;
                }
                catch (Exception ex)
                {
                    cn.Rollback();
                    throw new StockException(ex.Message);
                }
            }
        }

        public bool OpenAccount(int projectId, string tradingAccount, string tradingPassword)
        {
            WeekWinProject weekWinProject = new WeekWinProject
            {
                ProjectId = projectId
            };
            using (var cn = Environment.OpenSession())
            {
                if (!cn.Select(weekWinProject, WeekWinProject_.ALL, WeekWinProject_.User.ALL))
                {
                    return false;
                }
            }

            weekWinProject.TradingAccount = tradingAccount;
            weekWinProject.TradingPassword = tradingPassword;
            weekWinProject.ProjectState = (int)ProjectStateEnum.Trading;//操盘中的状态

            using (var cn = Environment.OpenSession())
            {
                cn.BeginTransaction();
                try
                {
                    bool a = cn.Update(weekWinProject, WeekWinProject_.ProjectState, WeekWinProject_.TradingAccount,
                         WeekWinProject_.TradingPassword) > 0;

                    User user = weekWinProject.User;
                    user.LockAmount -= weekWinProject.RiskMargin; //锁定金额
                    user.RiskAmount += weekWinProject.RiskMargin; //风险保证金
                    user.StockAmount += weekWinProject.FirmCapital;//配资金额
                    bool b = cn.Update(user) > 0;

                    if (a && b)
                    {
                        cn.Commit();
                        return true;
                    }
                    else
                    {
                        cn.Rollback();
                        return false;
                    }
                }
                catch (Exception ex)
                {
                    cn.Rollback();
                    throw new Exception(ex.Message);
                }
            }
        }

        //追加保证金
        public int AddRiskMargin(int projectId, int riskMargin, int type)
        {
            #region 查询当天是否已超过追加次数
            using (IDbSession cn = Environment.OpenSession())
            {
                object obj =
                    cn.ExecuteScalar(
                        "select count(*) from WeekWinProjectRiskMargin where date(addtime) = curdate() and ProjectId=" +
                        projectId);
                if (obj != null && (int)obj >= 5)
                {
                    return 2;
                }
            }
            #endregion

            using (var cn = Environment.OpenSession())
            {
                WeekWinProjectRiskMargin weekWinProjectRiskMargin = new WeekWinProjectRiskMargin
                {
                    ProjectId = projectId,
                    AddMoney = riskMargin,
                    AddState = 1,
                    AddTime = DateTime.Now,
                    Type = type
                };
                return cn.Insert(weekWinProjectRiskMargin);
            }
        }

        public bool Update(WeekWinProject weekWinProject,params object[] updateProperty)
        {
            try
            {
                using (var cn = Environment.OpenSession())
                {
                    return cn.Update(weekWinProject, updateProperty) > 0;
                }
            }
            catch (Exception ex)
            {
                throw new StockException(ex);
            }
        }

        public bool Delete(int id)
        {
            WeekWinProject weekWinProject = new WeekWinProject();
            {

            };
            try
            {
                using (var cn=Environment.OpenSession())
                {
                    return cn.Delete(weekWinProject) > 0;
                }
            }
            catch (Exception ex)
            {
                throw new StockException(ex.Message);
            }
        }

        #endregion


        #region 查询

        public WeekWinProject GetWeekWinProject(int id)
        {
            WeekWinProject weekWinProject = new WeekWinProject()
            {
                ProjectId = id
            };
            try
            {
                using (var cn = Environment.OpenSession())
                {
                    if (cn.Select(weekWinProject))
                    {
                        return weekWinProject;
                    }
                    return null;
                }
            }
            catch (Exception ex)
            {
                throw new StockException(ex.Message);
            }
        }


        public WeekWinProjects GetWeekWinProjects(QueryExpression query)
        {
            try
            {
                using (var cn = Environment.OpenSession())
                {
                    WeekWinProjects weekWinProjects = new WeekWinProjects();
                    weekWinProjects.TotalCount = cn.SelectCollection(weekWinProjects.Items, query);
                    return weekWinProjects;
                }
            }
            catch (Exception ex)
            {
                throw new StockException(ex.Message);
            }
        }

        #endregion

    }
}
